<?php

class Mesa extends MY_Model {
 
    function Mesa()
    {
        // Call the Model constructor
        parent::MY_Model();

	$this->nombreTabla = "mesa";
	$this->idT1= "idMesa";
	$this->nombreCampo="numMesa";


		$this->campos[] = array("realname" => "idSalon",
					"clase" => "partof",
					"label" => "Salon",
					"modelo" => "Salon",
					"rules" => "integer");
		
		$this->campos[] = array("realname" => "numMesa",
					"clase" => "text",
					"label" => "Numero de mesa",
					"rules" => "required|integer");
		
		$this->campos[] = array("realname" => "posX",
					"clase" => "text",
					"label" => "Posicion X",
					"rules" => "required|integer");

		$this->campos[] = array("realname" => "posY",
					"clase" => "text",
					"label" => "Posicion Y",
					"rules" => "required|integer");

		$this->campos[] = array("realname" => "tamX",
					"clase" => "text",
					"label" => "Ancho",
					"rules" => "required|integer");

		$this->campos[] = array("realname" => "tamY",
					"clase" => "text",
					"label" => "Largo",
					"rules" => "required|integer");

		$this->campos[] = array("realname" => "capacidadMaxima",
					"clase" => "text",
					"label" => "Capacidad maxima",
					"rules" => "required|integer");

		$this->campos[] = array("realname" => "capacidadMinima",
					"clase" => "text",
					"label" => "Capacidad minima",
					"rules" => "required|integer");

		$this->campos[] = array("realname" => "habilitada",
					"clase" => "text",
					"label" => "Habilitada",
					"rules" => "integer|exact_length[1]");

    }


    function getData($idResto, $fecha = null, $hora = '%')
	{
		if (!empty($fecha)){
			$dias = array('domingo', 'lunes', 'martes', 'miercoles', 'jueves', 'viernes', 'sabado');
			$diasemana=$dias[date("w", strtotime($fecha))];
			$query=$this->db->query("select mesa.*,salon.*, CASE  WHEN idReserva is  NULL THEN 0 ELSE 1 END as ocupada FROM mesa JOIN salon on salon.idSalon=mesa.idSalon JOIN salonfecha on salon.idSalon=salonfecha.idSalon left join (select idReserva,idMesa from reserva WHERE fecha='$fecha' and hora between addtime('$hora',-(select tiempoReserva from restaurant where idResto=$idResto)) and addtime('$hora',(select tiempoReserva from restaurant where idResto=$idResto))) reserva on (mesa.idMesa = reserva.idMesa) WHERE fechaDesde <= '$fecha' and fechaHasta >= '$fecha' and $diasemana is true and mesa.idSalon=$idResto");
		}else{
			$query=$this->db->query("select mesa.*,salon.*,CASE  WHEN idr is  NULL THEN 0 ELSE 1 END as ocupada FROM mesa JOIN salon on salon.idSalon=mesa.idSalon left join (select sum(idReserva) idr,idMesa from reserva WHERE  fecha>now() GROUP BY idMesa) reserva on (mesa.idMesa = reserva.idMesa) WHERE mesa.idSalon=$idResto");
		}
		if ($query->num_rows() > 0)
		{
			$auxValores=array();	

			$resultados=$query->result_array();
			if ($this->keyField) {
				return $resultados;
			} else { // Devolver con autonumeracion
				foreach ($resultados as $resultado){
					$auxCampos=array();
					foreach ($this->campos as $campo){
						$auxCampos[]=$resultado[$campo['realname']];
					}
					$auxCampos[]= $resultado[$this->idT1];
					$auxValores[]=$auxCampos;
				}	
				return $auxValores;
			}
		}else{
			return array();
		}
	}
     
}


?>
